package com.whc.gulimall.product.app;

import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.common.valid.AddGroup;
import com.whc.common.valid.UpdateGroup;
import com.whc.common.valid.UpdateStatusGroup;
import com.whc.gulimall.product.entity.BrandEntity;
import com.whc.gulimall.product.service.BrandService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:18:53
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {

    /**
     * 品牌 Service
     */
    @Resource
    private BrandService brandService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     *
     * @param brand
     * @return
     */
    @PostMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {
        brandService.save(brand);
        return R.ok();
    }


    /**
     * 修改品牌属性及关系表
     *
     * @param brand 品牌
     * @return 统一返回结果
     */
    @PutMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateCascade(brand);
        return R.ok();
    }

    /**
     * 更新状态
     *
     * @param brand 对象
     * @return 结果
     */
    @PutMapping("/update/status")
    public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateStatus(brand.getBrandId(), brand.getShowStatus());
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }

    @GetMapping("/infos")
    R infos(@RequestParam("brandIds") List<Long> brandIds) {
        List<BrandEntity> data = brandService.listByIds(brandIds);
        return R.ok().setData(data);
    }
}
